package com.tjise.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tjise.commonutils.JwtUtils;
import com.tjise.commonutils.MD5;
import com.tjise.educenter.entity.UcenterMember;
import com.tjise.educenter.entity.vo.RegisterVo;
import com.tjise.educenter.mapper.UcenterMemberMapper;
import com.tjise.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tjise.servicebase.exceptionhandler.TjiseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Random;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author TJISE
 * @since 2021-06-30
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    //登录的方法
    @Override
    public String login(UcenterMember member) {

        //获取登录手机号和密码
        String mobile = member.getMobile();
        String password = member.getPassword();

        //手机号和密码非空判断
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new TjiseException(20001,"登录失败");
        }

        //判断手机号是否正确
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember mobileMember = baseMapper.selectOne(wrapper);

        //判断查询对象是否为空
        if (mobileMember == null){//没有这个手机号
            throw new TjiseException(20001,"登录失败");
        }

        //判断密码
        //因为存储到数据库密码肯定是加密的
        //把输入的密码进行加密 再和数据库密码进行比较
        //加密方式 MD5
        if (!MD5.encrypt(password).equals(mobileMember.getPassword())){
            throw new TjiseException(20001,"登录失败");
        }

        //判断用户是否禁用
        if (mobileMember.getIsDisabled()){
            throw new TjiseException(20001,"登录失败");
        }

        //登录成功
        //生成token字符串 使用jwt工具类
        String jwtToken = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());

        return jwtToken;
    }

    //注册的方法
    @Override
    public void register(RegisterVo registerVo) {

        //获取注册的数据
        String nickname = registerVo.getNickname();//昵称
        String mobile = registerVo.getMobile();//手机号
        String password = registerVo.getPassword();//密码
        String code = registerVo.getCode();//验证码

        //非空判断
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password) || StringUtils.isEmpty(nickname) || StringUtils.isEmpty(code)){
            throw new TjiseException(20001,"注册失败");
        }

        //判断验证码
        String mobileCode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(mobileCode)){
            throw new TjiseException(20001,"注册失败");
        }

        //判断手机号是否重复 表里面存在相同手机号不进行添加
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if(count > 0) {
            throw new TjiseException(20001,"注册失败");
        }

        //数据添加数据库
        UcenterMember member = new UcenterMember();
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);
        member.setAvatar("https://tjiseedu.oss-cn-beijing.aliyuncs.com/%E5%89%8D%E5%8F%B0%E5%A4%B4%E5%83%8F/%E6%B5%B7%E8%B4%BC%E7%8E%8B.png");
        baseMapper.insert(member);
    }

    public int random(){
        Random random = new Random();
        return random.nextInt(9999);
    }
}
